package cs340.stormtroopers.junitTests.phase4BlackBox;

import junit.framework.*;
import cs340.stormtroopers.hypeerweb.*;
import cs340.stormtroopers.junitTests.*;

/**
 * Black Box test for the HyPeerWeb's Broadcast.
 * Exhaustively tests broadcasting from every node.
 * 
 * @author James Gurr
 */
public class BroadcastTest extends TestCase
{
	public static Test suite() 
	{
		return new TestSuite(BroadcastTest.class);
	}
	
	public BroadcastTest()
	{
		super();
	}
	
	public void setUp() 
	{
		hypeerweb = HyPeerWeb.getSingleton();
		first = new Node(0);
		hypeerweb.addNode(first);
	}
	
	public void tearDown() 
	{
		hypeerweb.clear();
	}
	
	private final int TEST_SIZE = 320;
	private HyPeerWeb hypeerweb;
	private Node first;
	
	/**
	 * Exhaustive test for broadcast.
	 * 
	 * Starting with a HyPeerWeb of size 1, add nodes to the HyPeerWeb until the limit is reached.
	 * After adding a node, for every node in the HyPeerWeb: create a broadcast visitor
	 *   with a unique message, and broadcast it across the HyPeerWeb.
	 * Then go through every node in the HyPeerWeb and verify that it received the message.
	 */
	public void testBroadcast() 
	{
		for (int i = 0; i < TEST_SIZE; ++i)
		{
			hypeerweb.clear();
			first = new Node(0);
			hypeerweb.addToHyPeerWeb(new Node(i), first);
			
			for (int j = 0; j < hypeerweb.size(); ++j)
			{
				BroadcastVisitor broadcaster = new ConcreteBroadcastVisitor();
				Parameters param = broadcaster.createInitialParameters();
				param.set("message", i + " " + j);
				hypeerweb.getNode(j).accept(broadcaster, param);

				for (int k = 0; k < hypeerweb.size(); ++k)
				{
					Node node = hypeerweb.getNode(k);
					assertTrue(node.getContents().containsValue(i + " " + j));
				}
			}
		}
	}
	
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}
	
}
